// var mongoose = require('mongoose');

// const Comment = require('../models/comment.model');

// exports.create = function(req,res,next){
// 	const comment = new Comment(req.body);

// 	comment.save()
// 	.then(data=>{
// 		 res.json(data);
// 	})
// }

// exports.get = function(req,res,next){
// 	var id = req.params.id;

// 	Comment.findById(id, function (err, data) {
// 	  res.json(data)
// 	})
// }

// exports.update = function(req,res,next){
// 	// const comment = new Comment(req.body);
// 	const id = req.params.id;

// 	Comment.findByIdAndUpdate(id, {$set:req.body}, {new:false})
// 	.then(data=>{
// 		res.json(data);
// 	})
// }

// exports.remove = function(req,res,next){
// 	const id = req.params.id;

// 	Comment.findByIdAndRemove(id,function(err,data){
// 		res.json(data)
// 	})
	
// }

// exports.list = function(req,res,next){
// 	var page = (req.body.page) ? req.body.page : 1;
// 	var rows = (req.body.rows) ? req.body.rows : 5;

// 	var queryCondition = {};
// 	if(req.body.name && req.body.name.trim().length>0){
// 		name = req.body.name;
// 		queryCondition = {
// 			"name" : new RegExp(name,'i')
// 		}
// 	}

// 	if(req.body.cateid && req.body.cateid.trim().length>0){
// 		cateid = req.body.cateid;
// 		queryCondition = {
// 			"cateid" : cateid
// 		}
// 	}
// 									//倒序sort
// 	Comment.paginate(queryCondition, {sort: { _id: -1}, page: parseInt(page), limit: parseInt(rows) }, function(err, result) {
// 	  result.rows = result.docs;
// 	  delete result.docs;

// 	  res.json(result)	
// 	});
	
// }


// exports.deletes = function(req,res,next){
// 	// console.log(req.body)
// 	var ids = req.body.ids;

// 	if(ids.length>0){
// 		console.log(ids.split(','))
// 		Comment.remove({_id:{$in:ids.split(',')}})
// 		.then(data=>{
// 			res.json({"msg":"deletes success","status":200})
// 		})
// 	}else{
// 			res.json({"msg":"deletes fail","status":404})

// 	}
// }












// // exports.list = function(req,res,next){
// //     var page = (req.query.page) ? req.query.page : 1;
// //     var rows = (req.query.rows) ? req.query.rows : 10;

// //     var queryCondition = {};
// //     var name;
// //     if( req.query.name 

// //  && req.query.title.trim().length>0 ){
// //         name = req.query.name 

// // ;
// //         queryCondition = {"name":new RegExp(name,'i')}
// //     }
// //     Comment.paginate(queryCondition, {sort:{_id: -1}, page: parseInt(page), limit: parseInt(rows)}, function(err, result) {
// //         result.rows = result.docs;
// //         delete result.docs;
// //         res.json(result);
// //     });
// // }


// // exports.list = function(req,res,next){
// // 	var page = (req.body.page) ? req.body.page : 1;
// // 	var rows = (req.body.rows) ? req.body.rows : 5;

// // 	var queryCondition = {};
// // 	if(req.body.name && req.body.name.trim().length>0){
// // 		name = req.body.name;
// // 		queryCondition = {
// // 			"name" : new RegExp(name, 'i')
// // 		}
// // 	}

// // 	DataModel.paginate(queryCondition, {sort: { _id : -1 }, page: parseInt(page), limit: parseInt(rows) }, function(err, result) {
// // 	 	result.rows = result.docs;
// // 	 	delete result.docs;
// // 	 	res.json(result)
// // 	});
	
// // }




var mongoose = require('mongoose');
var Comment = require('../models/comment.model.js')// 引数据模型


// 新增
exports.create = function(req,res,next){
    var comment = new Comment(req.body); // 从req.body里获取参数对象，比如{name:'vane',gender:'男',city:'杭州'....}

    comment.save()
    .then(data=>{
        res.json(data)
    })
    
}
exports.get = function(req,res,next){
    var id =req.params.id; // 从req.body里获取参数对象，比如{name:'vane',gender:'男',city:'杭州'....}
    console.log(id);
    Comment.findById(id,function(err,result){
        console.log(result);
        res.json(result);
    });
}
//修改
exports.update = function(req,res,next){
  
    var id = req.params.id; // req.param('id')
    // 现在操作的都是mongoose而不是mongodb
    Comment.findByIdAndUpdate(id,{$set: req.body}, {new:false} )
    .then(data=>{
        res.json({msg:'数据修改成功',status:200}); // 如果你要返回数据，这个data返回的是你之前修改的那个数据内容
    })
}

//删除
exports.remove = function(req,res,next){
    var id = req.params.id;
    Comment.findByIdAndRemove(id,function(err,doc){
        res.json({msg:'数据删除成功',status:200})
    })
} 
exports.removes = function(req,res,next){
    var ids = req.params.ids;
    ids=ids.split(',');
    console.log(ids);
    Comment.remove({_id:{$in:ids}}).then(data=>{
        res.json(data);
    })
} 

exports.list = function(req,res,next){
    
    var page = (req.body.page) ? req.body.page : 1;
    // var limit = (req.body.limit) ? req.body.limit : 2;
    var rows = (req.body.rows) ? req.body.rows : 5;
    //console.log(rows,typeof rows);

    var queryCondition = {};
   
   
    if(req.body.userId && req.body.userId.trim().length>0){
        userId = req.body.userId;
        queryCondition = {
            "userId" : userId
        }
    }
    if(req.body.content && req.body.content.trim().length>0){
        content = req.body.content;
        queryCondition = {
            "content" : new RegExp(content,'i')
        }
    }

    if(req.body.newsId && req.body.newsId.trim().length>0){
        newsId = req.body.newsId;
        queryCondition = {
            "newsId" : newsId
        }
    }
    if(req.body.cateId && req.body.cateId.trim().length>0){
        cateId = req.body.cateId;
        queryCondition = {
            "cateId" : cateId
        }
    }


    Comment.paginate(queryCondition, { page: page, limit: parseInt(rows) }, function(err, result) {
        result.rows = result.docs;
        delete result.docs;

       res.json(result)
    });
}
